using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
/*
 * Copyright � 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using IAPRepo_WebRole.Models;

namespace IAPRepo_WebRole
{
    public class WebRole : RoleEntryPoint
    {
        public static string DATA_CONNECTION_STRING = "DataConnectionString";
        public override bool OnStart()
        {
            var account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(WebRole.DATA_CONNECTION_STRING));

            // Initialize cloud Table storage
            account.CreateCloudTableClient().CreateTableIfNotExist(IAPProduct.TABLE_NAME);
            account.CreateCloudTableClient().CreateTableIfNotExist(IAPTicket.TABLE_NAME);

            // Initialize cloud Blob storage
            var blobClient = account.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(IAPProduct.BLOB_CONTAINER_NAME);
            container.CreateIfNotExist();

            // Set required permissions to access the blobs publicly
            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);

            return base.OnStart();
        }
    }
}
